﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Configuration;

namespace AnguaRPGEngine.Library.Forms
{
    public partial class BookListForm : Form
    {
        private List<RuleBook> _Books;
        public List<RuleBook> Books
        {
            get
            {
                return _Books;
            }
        }
        public BookListForm(List<RuleBook> books)
        {
            _Books = books;
            if (_Books == null)
            {
                _Books = new List<RuleBook>();
            }
            InitializeComponent();
            btnEdit.Enabled = btnDelete.Enabled = false;
            DisplayBooks();
        }

        private void DisplayBooks()
        {
            lvBooks.Items.Clear();
            foreach (RuleBook book in _Books)
            {
                lvBooks.Items.Add(book.BookName);
            }
        }
        private void lvBooks_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnEdit.Enabled = btnDelete.Enabled = lvBooks.SelectedItems.Count > 0;
        }
        #region Overrides For Background Painting And Resizing

        //const int WM_NCHITTEST = 0x0084;
        //const int HTCLIENT = 1;
        //const int HTCAPTION = 2;
        //protected override void WndProc(ref Message m)
        //{
        //    base.WndProc(ref m);
        //    switch (m.Msg)
        //    {
        //        case WM_NCHITTEST:
        //            if (m.Result == (IntPtr)HTCLIENT)
        //            {
        //                m.Result = (IntPtr)HTCAPTION;
        //            }
        //            break;
        //    }
        //}
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(this.Width, 15), Color.DarkBlue, Color.Blue), new Rectangle(0, 0, this.Width, 15));
        //    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.DarkGray), 1f), new Point(0, 16), new Point(this.Width, 16));
        //    e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.White), new PointF(0, 0));

        //}
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.Style |= 0x40000;

        //        return cp;
        //    }
        //}
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (BookEditor frmBookEditor = new BookEditor(null))
            {
                if (frmBookEditor.ShowDialog() == DialogResult.OK)
                {
                    _Books.Add(frmBookEditor.Book);
                    DisplayBooks();
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            using(BookEditor frmBookEditor = new BookEditor(_Books.Find(p => p.BookName == lvBooks.SelectedItems[0].Text)))
            {
                if(frmBookEditor.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    _Books.Find(p=>p.BookName == lvBooks.SelectedItems[0].Text).Bookmarks  = frmBookEditor.Book.Bookmarks;
                    _Books.Find(p=>p.BookName == lvBooks.SelectedItems[0].Text).BookName = frmBookEditor.Book.BookName;
                    DisplayBooks();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show(this, "Are you certain that you wish to delete this book?", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                _Books.RemoveAt(lvBooks.SelectedItems[0].Index);
                DisplayBooks();
            }
        }

    }
}
